<?php
  require_once('./includes/header.php');

  if(isset($_SESSION['id']) && !empty($_SESSION['id'])){
    header("location:show_all_users.php");
    exit;
  }


  if(isset($_GET['hash']) && !empty($_GET['hash']))
  {
    $hash = trim($_GET['hash']);
    $hash = decrypt($hash);
    $hash = explode('#',$hash);
    //pr1($hash);
    //pr1(time());
    if(time() > $hash[1] ){
      $msg = "Invalid  or link has been expired1.";
    } else {
      /*******************************/
      $ans = fetchById('users',$hash[0]);
      //pr1($ans);
      /*******************************/
      if(isset($ans['id']) ){

      } else {
        $msg = "Invalid  or link has been expired2.";
      }
    }
    //pr($rows);
  }

  if(isset($_POST['password']) && !empty($_POST['password'])){
    if($_POST['password'] == $_POST['cpassword']){
      $password = md5(trim($_POST['password']));
      updateById('users',$_POST['id'],array('password'=>$password));
      $msg = "Password has been updated successfully.";

    } else {
      $msg = "Password and Confirm Password didn't match.";
    }
  }


?>
<!------------------HEADER ENDS-------------------------->

      <div class="row">

<?php
if($msg != '') {
?>
    <div class="alert alert-danger fade in">
      <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
      <p><?=$msg?></p>
    </div>
<?php
}
?>

        <h2 class="form-signin-heading">Please enter your registered email id</h2>

<!----------------  FORM ------------>
      <form method="post" class="form-signin" role="form">
        <input type="hidden" name="id" value="<?=$ans['id']?>" />

        <input name="password" autocomplete="off" value="" type="password" class="form-control" placeholder="Password"  autofocus>
        <input name="cpassword" autocomplete="off" value="" type="password" class="form-control" placeholder="Confirm Password"  autofocus>
        <br>
        <button class="btn btn-lg btn-primary btn-block" type="submit">Update Password </button>
        <a href="login.php">Login</a>
      </form>

<!----------------  FORM ------------>
      </div>
<!-------------- FOOTER START --------------------------->
    <!-- Custom styles for this template -->
    <link href="css/signin.css" rel="stylesheet">
<?php
  require_once('./includes/footer.php');
?>
